Server-Side WebAssembly by Danilo Chiarlone
Author:Danilo Chiarlone [Danilo Chiarlone]
Language: eng
Format: epub
ISBN: 9781633436206
Publisher: Manning Publications Co.
Published: 2025-08-05T00:00:00+00:00
Overall, the full code can be seen in listing 5.9.
Listing 5.9 lib.rs file from smartcms_ml_guest
#[allow(warnings)] mod bindings; use tokenizers::Tokenizer; use rand::prelude::*; use ndarray::Array; use std::fs; use bindings::{Guest, wasi::nn::{ graph::{Graph, load, ExecutionTarget, GraphEncoding}, tensor::{Tensor, TensorType}, }}; struct Component; impl Guest for Component { fn storygen() -> String { let model_data = fs::read("fixture [CA]/modified_model.onnx").unwrap(); #A let graph = load(&[model_data], [CA]GraphEncoding::Onnx, ExecutionTarget [CA]::Cpu).unwrap(); #A let exec_context = Graph::init_execution_ [CA]context(&graph).unwrap(); #A let tokenizer = [CA]Tokenizer::from_file("fixture/ [CA]tokenizer.json").unwrap(); #B let prompt = "Once upon a time"; #C let encoding = tokenizer. [CA]encode(prompt, true).unwrap(); #C let mut input_ids = [CA]encoding.get_ids().to_vec(); #C let mut rng = thread_rng(); #D let eos_token_id = tokenizer.token_ [CA]to_id("<EOS>").unwrap_or(0); #D for _ in 0..100 { #E let sequence_length = input_ids.len(); #F let dimensions = vec! [CA][1, sequence_length as u32]; #F let input_f32: Vec<f32> = input_ [CA]ids.iter().map(|&id| id as f32).collect(); #F let mut input_data = Vec:: [CA]with_capacity(input_f32.len() * 4); #F for &val in &input_f32 { #F input_data.extend_from_slice [CA](&val.to_ne_bytes()); #F } #F let tensor = Tensor::new [CA](&dimensions, TensorType::Fp32, &input_data); #F exec_context.set_input [CA]("input_ids", tensor).unwrap(); #F exec_context.compute().unwrap(); #G let output_data = exec_context. [CA]get_output("logits").unwrap().data(); #H let output_f32 = bytes_to_f32_vec [CA](&output_data); #H let vocab_size = output_f32. [CA]len() / sequence_length; #I let start = (sequence_length - 1) [CA]* vocab_size; #I let end = sequence_length * vocab_size; #I let last_token_logits = [CA]&output_f32[start..end]; #I let probabilities = softmax [CA](last_token_logits); #J let dist = rand::distributions:: [CA]WeightedIndex::new(&probabilities).unwrap(); #K let next_token = dist.sample(&mut rng); #K input_ids.push(next_token as u32); #K let generated_text = [CA]tokenizer.decode(&input_ids, true).unwrap(); #L if generated_text.ends_with("
") { #M break; #M } #M if next_token as u32 == eos_token_id { #M break; #M } #M } tokenizer.decode(&input_ids, true) [CA].unwrap().trim().to_string() #N } } pub fn bytes_to_f32_vec(data: &[u8]) -> Vec<f32> { #O data.chunks_exact(4) .map(|c| f32::from_le_bytes(c.try_into().unwrap())) .collect() } fn softmax(logits: &[f32]) -> Vec<f32> { #P let logits_array = Array::from_vec(logits.to_vec()); let exp_logits = logits_array.mapv(f32::exp); let sum_exp = exp_logits.sum(); (exp_logits / sum_exp).to_vec() } bindings::export!(Component with_types_in bindings);
#A Loading the ONNX model and setting up the graph and execution context
#B Loading the tokenizer from the provided file
#C Defining the prompt and encoding it into token IDs
#D Initializing the random number generator and setting the EOS token
#E Looping up to 100 iterations to generate tokens
#F Preparing and setting the input tensor
#G Running the model inference
#H Retrieving and converting the modelâs output
#I Extracting the logits for the last token in the sequence
#J Applying softmax to calculate token probabilities
#K Sampling the next token based on probabilities and appending it to the sequence
#L Decoding the generated text
#M Breaking the loop if a natural end to the story is detected
#N Returning the final generated story
#O The function to convert bytes to f32 values
#P The softmax function to compute the probability distribution
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(154172)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74273)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(66077)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(65825)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(65263)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50858)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(37760)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(37672)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(37493)
What's Done in Darkness by Kayla Perrin(27103)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26483)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26435)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(21014)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20774)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20648)
The Fifty Shades Trilogy & Grey by E L James(19604)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19487)
Shot Through the Heart by Mercy Celeste(19347)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17490)